package chen.web.user;

import java.util.List;

import chen.web.MyContext;
import chen.web.MyContextProvider;

public class SecurityAccountManager implements AccountManager {

	private AccountManager am = null;
	
	public SecurityAccountManager(AccountManager am){
		this.am = am;
	}
	
	private AccountPermissionConfig config(){
		return PermissionConfigRepository.loadCurrent(AccountPermissionConfig.class);
	}
	
	@Override
	public Account changeEmail(Account account, String newEmail)
			throws EmailAlreadyBeenRegisteredException {
		// TODO Auto-generated method stub
		if(!config().isChangeEmail()){
			throw new NoPermissionException();
		}
		return am.changeEmail(account, newEmail);
	}
	@Override
	public Account changeEmail(String email, String password)
			throws InvalidPasswordException,
			EmailAlreadyBeenRegisteredException {
		// TODO Auto-generated method stub
		return am.changeEmail(email, password);
	}
	@Override
	public Account changePassword(String newPassword, String oldPassword)
			throws InvalidPasswordException {
		// TODO Auto-generated method stub
		return am.changePassword(newPassword, oldPassword);
	}
	@Override
	public Account changePassword(Account account, String newPassword) {
		// TODO Auto-generated method stub
		if(!config().isChangePassword()){
			throw new NoPermissionException();
		}
		return am.changePassword(account, newPassword);
	}
	@Override
	public Account changeRole(Account account, Role role) {
		// TODO Auto-generated method stub
		if(!config().isChangeRole()){
			throw new NoPermissionException();
		}
		return am.changeRole(account, role);
	}
	@Override
	public void deleteAccount(Account account) {
		// TODO Auto-generated method stub
		if(!config().isDeleteAccount()){
			throw new NoPermissionException();
		}
		am.deleteAccount(account);
	}
	@Override
	public Account getAccount(int id) {
		// TODO Auto-generated method stub
		return am.getAccount(id);
	}
	@Override
	public List<Account> getAccount(List<Integer> ids) {
		// TODO Auto-generated method stub
		return am.getAccount(ids);
	}
	@Override
	public Account getAccountByEmail(String email) {
		// TODO Auto-generated method stub
		return am.getAccountByEmail(email);
	}
	@Override
	public Account getAccountByName(String nickname) {
		// TODO Auto-generated method stub
		return am.getAccountByName(nickname);
	}
	@Override
	public Account increaseRank(Account account, int increase) {
		// TODO Auto-generated method stub
		if(!config().isIncreaseRank()){
			throw new NoPermissionException();
		}
		return am.increaseRank(account, increase);
	}
	@Override
	public void mergeAccount(Role oldRole, Role newRole) {
		// TODO Auto-generated method stub
		if(!config().isMergeAccount()){
			throw new NoPermissionException();
		}
		am.mergeAccount(oldRole, newRole);
	}
	@Override
	public Account register(String nickname, String password, String email)
			throws NicknameAlreadyBeenRegisteredException,
			EmailAlreadyBeenRegisteredException {
		// TODO Auto-generated method stub
		return am.register(nickname, password, email);
	}
	@Override
	public int totalAccount() {
		// TODO Auto-generated method stub
		return am.totalAccount();
	}
	@Override
	public void update(Account account) {
		// TODO Auto-generated method stub
		if(config().isUpdateAccount()){
			am.update(account);
		}else if(config().isUpdateOwnAccount() && account.equals(MyContextProvider.getMyContext().getAccount())){
			am.update(account);
		} else {
			throw new NoPermissionException();
		}
	}
}
